package com.example.factory.api.controller;

import com.example.factory.api.dto.DtoUser;
import com.example.factory.api.dto.Response;
import com.example.factory.api.dto.form.DtoQueryUserListForm;
import com.example.factory.api.dto.form.DtoUserForm;
import com.example.factory.api.facade.UserFacade;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lei Tao
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {
    private final UserFacade userFacade;

    public UserController(UserFacade userFacade) {
        this.userFacade = userFacade;
    }

    @PostMapping("/list")
    public Response<List<DtoUser>> list(@RequestBody DtoQueryUserListForm dtoQueryUserListForm) {
        return userFacade.retrieveUsers(dtoQueryUserListForm);
    }

    @GetMapping("/{userGuid}/details")
    public Response<DtoUser> details(@PathVariable("userGuid") String userGuid) {
        return userFacade.retrieveUser(userGuid);
    }

    @GetMapping("/{userGuid}/delete")
    public Response<Void> delete(@PathVariable("userGuid") String userGuid) {
        return userFacade.deleteUser(userGuid);
    }

    @PostMapping("/save")
    public Response<DtoUser> saveOrUpdate(@RequestBody DtoUserForm dtoUserForm) {
        return userFacade.saveOrUpdate(dtoUserForm);
    }

}
